package com.cyandpos.pad.refund.model;

import android.text.TextUtils;
import android.util.Log;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.refund.contract.RefundContract;
import com.cyandpos.pad.utils.log.MessageLog;
import com.google.gson.JsonObject;

public class RefundModelImpl implements RefundContract.Model {
    @Override
    public void getOldTicketInfo(final String oldSktno, String oldjlbh,boolean isOrderNum,final OnCompleteListener completeListener) {
        QueryOrderReq req = new QueryOrderReq();
        req.setSktno(oldSktno);
        if(isOrderNum){
            req.setOrderNo(oldjlbh);
        }else{
            req.setJlbh(oldjlbh);
        }
        req.setStatus("1");
        req.setQueryHistoryData(true);

        PosHttpManager.QueryOrderInfoEexcute(req, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                try {
                    BaseResponse<RefundOrder> baseResponse = BaseResponse.fromJson(response.toString(), RefundOrder.class);
                    if (baseResponse.getCode().equals("0")){
                        TicketInfo ticketInfo = new TicketInfo();
                        for (int i = 0; i < baseResponse.getData().getOrderList().size(); i++) {
                            ticketInfo.setTicket(baseResponse.getData().getOrderList().get(i));
                        }
                        completeListener.onSuccess(ticketInfo);
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            onFailure(baseResponse.getMsg());
                        }else {
                            onFailure(baseResponse.getTips());
                        }
                    }
                }catch (Exception e){
                    Log.e("TAG","e==="+e.toString());
                    onFailure("接口异常："+e.toString());
                    MessageLog.writeLog("接口异常："+e.toString());
                }
            }
            @Override
            public void onFailure(String msg) {
                completeListener.onFailure(msg+"......");
            }
        });
    }
}
